package com.tanglongan.concurrent._basic;

import lombok.AllArgsConstructor;
import lombok.Data;

public class Demo14 {
    public static void main(String[] args) throws InterruptedException {
        Demo14User user = new Demo14User();
        Demo14Thread t = new Demo14Thread(user);
        t.start();
        Thread.sleep(500);
        t.stop();
        System.out.println("username=" + user.getUsername() + ",password=" + user.getPassword());
    }
}


@AllArgsConstructor
class Demo14Thread extends Thread {
    private Demo14User user;

    @Override
    public void run() {
        user.updateUsernameAndPassword("b", "bb");
    }
}


@Data
class Demo14User {
    private String username;
    private String password;

    synchronized public void updateUsernameAndPassword(String username, String password) {
        try {
            this.username = username;
            Thread.sleep(10000);    //模拟其他耗时的业务逻辑处理
            this.password = password;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
